package test.zhang;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: gcr
 * @Date: 2022/8/20 10:23
 */
public class 给张旭关于ut对应年份统计 {
    @SneakyThrows
    public static void main(String[] args) {

        String dbName = "scholar.tb_true";
        String dbNameIns = "scholar.tb_true_year_ut";

        // 年份对应的ut集合
        Map<Integer, List<String>> yearAndUtList = new ConcurrentHashMap<>();

        // // vm options -Xmx18660m -Xms12560m
        int startPmid = 0;
        Db usetaiyi = Db.use(DSFactory.get("server_localscholar"));
        String allDate = "select max(pid) as pid  from " + dbName;
        Entity query = usetaiyi.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pid"));

        Integer count = query.getInt("pid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select pid,PY,UT from " + dbName + " where  pid between  " + start + " and " + end;
            List<Entity> querys = usetaiyi.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                Integer py = entity.getInt("PY");
                String UT = entity.getStr("UT");
                if (StringUtils.isNotEmpty(UT) && py != null && py.intValue() > 0) {
                    yearAndUtList.compute(py, (k, v) -> {
                        if (v == null) {
                            v = Collections.synchronizedList(new ArrayList<>(10000000));
                            v.add(UT);
                            return v;
                        } else {
                            v.add(UT);
                            return v;
                        }
                    });

                }
            });
        }
        SmallTool.printMess("全部数量+++" + yearAndUtList.size());
        yearAndUtList.forEach((k, v) -> {

            Long s1 = System.currentTimeMillis();
            SmallTool.printMess("开始执行年份++" + k + "++数量是++" + v.size());
            v.parallelStream().forEach(o -> {
                Entity entity = new Entity();
                entity.setTableName(dbNameIns);
                entity.set("PY", k);
                entity.set("UT", o);
                try {
                    usetaiyi.insert(entity);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            });
            SmallTool.printMessLongTimeTake("完成++", s1);

        });


    }
}
